package v3.chapter6.singleton_9;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class Run {
	public static void main(String[] args) {
		MyThread t1 = new MyThread();
		MyThread t2 = new MyThread();
		MyThread t3 = new MyThread();

		t1.start();
		t2.start();
		t3.start();
	}

}

class MyThread extends Thread {

	@Override
	public void run() {
		for (int i = 0; i < 5; i++) {
			System.out.println(MyObject.connectionFactory.getConnection());
		}
	}
}

enum MyObject{
	connectionFactory;
	
	private Connection connection;
	
	private MyObject() {
		try {
			System.out.println("调用了MyObject的构造");
			String url="jdbc:mysql://localhost:3306/credit_rating?useUnicode=true&useSSL=true&characterEncoding=utf8&characterSetResults=utf8";
			String username="credit_rating";
			String password="123456";
			String driverName="com.mysql.jdbc.Driver";
			Class.forName(driverName);
			connection=DriverManager.getConnection(url, username, password);
		}catch(ClassNotFoundException e) {
			e.printStackTrace();
		}catch(SQLException e) {
			e.printStackTrace();
		}
	}
	
	public Connection getConnection() {
		return connection;
	}
}